package com.lzs.chuku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lzs.entity.Outorder;
import com.lzs.entity.OutorderDetail;
import com.lzs.chuku.feign.KucunFeign;
import com.lzs.chuku.mapper.OutorderDetailMapper;
import com.lzs.chuku.mapper.OutorderMapper;
import com.lzs.chuku.service.OutorderDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-23
 */
@Service
public class OutorderDetailServiceImpl extends ServiceImpl<OutorderDetailMapper, OutorderDetail> implements OutorderDetailService {

    @Autowired
    private OutorderDetailMapper outorderDetailMapper;
    @Autowired
    private OutorderMapper outorderMapper;
    @Autowired
    private KucunFeign kucunFeign;

    @Autowired
    private BaseMapper<OutorderDetail> baseMapper;
    /*
    * 添加出货商品 减少相应仓库库存
    * 如果存在此商品 则加上后来增加的数量
    * 不存在 就新增明细
    * */
    @Override
    public R add(OutorderDetail outorderDetail) {
        try {
            Long commodityid = outorderDetail.getCommodityid();
            Integer num=outorderDetail.getQty();
            QueryWrapper<OutorderDetail> wrapper=new QueryWrapper<>();
            wrapper.eq("pid",outorderDetail.getPid());
            wrapper.eq("commodityid",outorderDetail.getCommodityid());
            OutorderDetail outorderDetail1 = outorderDetailMapper.selectOne(wrapper);
            Outorder outorder = outorderMapper.selectById(outorderDetail.getPid());
            R r = kucunFeign.updateQuantity(outorderDetail.getLocatorid(),outorderDetail.getCommodityid(),outorderDetail.getWarehouseid(),num);
            if (r.getSuccess()){
                if (outorderDetail1!=null){
                    outorderDetail.setQty(outorderDetail1.getQty()+outorderDetail1.getQty());
                    QueryWrapper<OutorderDetail> wrapper1=new QueryWrapper<>();
                    wrapper1.eq("id",outorderDetail.getId());
                    outorderDetailMapper.update(outorderDetail,wrapper);
                }else{
                    outorderDetailMapper.insert(outorderDetail);
                }
            }
            return r;
        }catch (Exception e){
            return R.error().message("添加失败");
        }
    }

    @Override
    public R findAllOUtorderdetailById(Long outorderid) {
        try {
            QueryWrapper<OutorderDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid", outorderid);
            List<OutorderDetail> utorderDetailList = outorderDetailMapper.selectList(queryWrapper);
            return R.ok().message("查询成功").data("utorderDetailList",utorderDetailList);
        }catch (Exception e){
            return R.error().message("查询成功");
        }
    }

    @Override
    public R findCommodityBarByourorderid(Long outorderid) {
        try {
            QueryWrapper<OutorderDetail> wrapper=new QueryWrapper<>();
            wrapper.eq("pid",outorderid);
            List<OutorderDetail> outorderDetails = baseMapper.selectList(wrapper);
            return R.ok().message("查询成功").data("commList",outorderDetails);
        }catch (RuntimeException e){
            return R.error().message("查询失败");
        }
    }
}
